package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.common.constant.PayCardLogContent;
import com.tbit.uqbike.object.pojo.PayCardLog;
import com.tbit.uqbike.object.pojo.dto.PayCardLogDownloadDTO;
import com.tbit.uqbike.object.pojo.dto.PayCardLogQueryDTO;
import com.tbit.uqbike.object.pojo.vo.PayCardLogSumVO;
import com.tbit.common.entity.view.PageResult;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.PayCardLogService;
import com.tbit.uqbike.webmanager.dao.capital.PayCardLogDao;
import com.tbit.uqbike.webmanager.util.BeanUtils;
import com.tbit.uqbike.webmanager.util.csv.CsvExportUtils;
import com.tbit.uqbike.webmanager.util.csv.entity.CsvBaseEntity;
import com.tbit.uqbike.webmanager.util.csv.entity.ExportHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author:BUGTian
 * @DATE: 2022/11/12
 */
@Service
public class PayCardLogServiceImpl extends ServiceImpl<PayCardLogDao, PayCardLog> implements PayCardLogService {
    @Autowired
    CsvExportUtils csvExportUtils;
    @Autowired
    AccountService accountService;
    @Autowired
    private PayCardLogDao payCardLogDao;

    @Override
    public PageResult<PayCardLog> queryPage(PayCardLogQueryDTO payCardLogQueryDTO) {
        Integer page = payCardLogQueryDTO.getPage();
        Integer size = payCardLogQueryDTO.getSize();
        PageInfo<PayCardLog> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(() -> payCardLogDao.query(payCardLogQueryDTO));
        return new PageResult<>(page, size, pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
    }

    @Override
    public PayCardLogSumVO sum(PayCardLogQueryDTO payCardLogQueryDTO) {
        Map<Integer, PayCardLog> map = payCardLogDao.sum(payCardLogQueryDTO);
        PayCardLogSumVO payCardLogSumVO = new PayCardLogSumVO();
        PayCardLog rideCard = map.get(PayCardLogContent.ride_card);
        PayCardLog vipCard = map.get(PayCardLogContent.vip_card);
        PayCardLog depositCard = map.get(PayCardLogContent.deposit_card);
        PayCardLog couponCard = map.get(PayCardLogContent.coupon_card);
        payCardLogSumVO
                .setRideCardMoney(rideCard == null ? 0 : rideCard.getOrderMoney())
                .setRideCardCount(rideCard == null ? 0 : rideCard.getTotalCount())
                .setVipCardMoney(vipCard == null ? 0 : vipCard.getOrderMoney())
                .setVipCardCount(vipCard == null ? 0 : vipCard.getTotalCount())
                .setDepositCardMoney(depositCard == null ? 0 : depositCard.getOrderMoney())
                .setDepositCardCount(depositCard == null ? 0 : depositCard.getTotalCount())
                .setCouponMealMoney(couponCard == null ? 0 : couponCard.getOrderMoney())
                .setCouponMealCount(couponCard == null ? 0 : couponCard.getTotalCount());
        payCardLogSumVO.compute();
        return payCardLogSumVO;
    }

    @Override
    public String downloadPayCard(PayCardLogDownloadDTO downloadDTO) {
        downloadDTO.setFileName("购卡订单统计账单");

        File tempFile = csvExportUtils.getTempFile();
        try {
            File file = downloadPayCard(downloadDTO, tempFile);
            String uploadFileName = UUID.randomUUID().toString().replaceAll("-", "");
            return csvExportUtils.uploadZip(tempFile, uploadFileName, file);
        } finally {
            FileUtil.del(tempFile);
        }
    }

    @Override
    public File downloadPayCard(PayCardLogDownloadDTO downloadDTO, File tempFile) {
        ExportHandler exportHandler = new ExportHandler() {
            @Override
            public List handle(CsvBaseEntity baseEntity) {
                PayCardLogQueryDTO dto = BeanUtils.copyProperties(downloadDTO, PayCardLogQueryDTO.class);
                dto.setStartTime(baseEntity.getStartTime());
                dto.setEndTime(baseEntity.getEndTime());
                List<PayCardLog> list = payCardLogDao.query(dto);
                return list;
            }
        };
        Date startTime = downloadDTO.getStartTime();
        Date endTime = downloadDTO.getEndTime();
        return csvExportUtils.export(exportHandler, PayCardLog.class, startTime, endTime, tempFile, downloadDTO.getFileName());
    }

}
